import store from '../index.js'
export default {
    state: {
        news_history: [],
        goods_history: [],
    },
    mutations: {
        setGoodsHistory(state, key) {
            state.goods_history.push(key)
        },
        clearGoodsHistory(state, value) {
            state.goods_history = clearHistory(state.goods_history, value)
        },
    },
    actions: {
        setGoodsHistory({
            commit
        }, key) {
            commit('setGoodsHistory', key)
        },
        clearGoodsHistory({
            commit
        }, key) {
            commit('clearGoodsHistory', key)
        }
    },
    getters: {
        getGoodsSearchList: state => () => {
            return Array.from(new Set(state.goods_history))
        }
    }
}

function clearHistory(history, value) {
    let localHistory = history
    if ((value ?? '') !== '') {
        localHistory.map((item, index) => {
            if (item == value) {
                localHistory.splice(index, 1)
            }
        })
    } else {
        localHistory = []
    }
    return localHistory
}
